home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Text / Send Note Ext folder / Note.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-25  |  2.4 KB  |  106 lines  |  [TEXT/KAHL]

  1. // Send Note External for BBEdit
  2. //    v1.0 ©1993 by Mike Cohen, ISIS International
  3. //    isis@netcom.com
  4. //
  5.  
  6. #include "ExternalInterface.h"
  7. #include <AppleEvents.h>
  8. #include <Packages.h>
  9. #include <SetupA4.h>
  10.  
  11. #define    kNoteClass    'IS07'
  12. #define    kChoose        'CHOS'
  13. #define    kSendNote    'SEND'
  14. #define    keyUser        'USER'
  15.  
  16. pascal void main(ExternalCallbackBlock *cb, WindowPtr w)
  17. {    long selStart, selEnd, firstChar;
  18.     AppleEvent aevt = {0}, reply = {0};
  19.     AEAddressDesc addr = {0};
  20.     char name[256], *p;
  21.     DescType actualType;
  22.     Size actualSize;
  23.     char hstate;
  24.     OSErr err;
  25.     Handle h;
  26.     int i;
  27.  
  28.     RememberA0();
  29.     SetUpA4();
  30.  
  31.     // get text to be sent
  32.     // use ONLY the selection if any, otherwise use the whole thing
  33.     h = (cb->GetWindowContents)(w);
  34.     (cb->GetSelection)(&selStart, &selEnd, &firstChar);
  35.     if (selStart == selEnd)
  36.         {
  37.         selStart = 0;
  38.         selEnd = GetHandleSize(h);
  39.         }
  40.  
  41.     hstate = HGetState(h);
  42.     MoveHHi(h);
  43.     HLock(h);
  44.     p = (*h)+selStart;
  45.  
  46.     // first create an address descriptor for ISIS Notes™
  47.     err = AECreateDesc(typeApplSignature,"IS07",4,&addr);
  48.     if (err == 0)
  49.         {
  50.         // if the text starts with a "To:" line, use that
  51.         if (IUMagIDString(p,"To: ",4,4) == 0)
  52.             {
  53.             i = 1;
  54.             p += 3;
  55.             while (*p == 32)
  56.                 ++p;
  57.             while (*p != 13)
  58.                 name[i++] = *p++;
  59.             name[0] = i-1;
  60.  
  61.             }
  62.         else
  63.             {
  64.             // otherwise, ask ISIS Notes™ to select a recipient
  65.             if (err = AECreateAppleEvent(kNoteClass,kChoose,&addr,
  66.                     kAutoGenerateReturnID,kAnyTransactionID,&aevt)) goto Done;
  67.  
  68.             if (err = AESend(&aevt,&reply,
  69.                     kAEWaitReply+kAEAlwaysInteract+kAECanSwitchLayer,
  70.                     kAENormalPriority,kNoTimeOut,NULL,NULL)) goto Done;
  71.  
  72.             if (err = AEGetParamPtr(&reply,keyDirectObject,typeChar,
  73.                     &actualType,&name[1],255,&actualSize)) goto Done;
  74.  
  75.             name[0] = (actualSize > 255) ? 255 : actualSize;
  76.             AEDisposeDesc(&aevt);
  77.             AEDisposeDesc(&reply);
  78.             }
  79.  
  80.         // now that we have an address, let's send the note
  81.         if (err = AECreateAppleEvent(kNoteClass,kSendNote,&addr,
  82.             kAutoGenerateReturnID,kAnyTransactionID,&aevt)) goto Done;
  83.  
  84.         if (err = AEPutParamPtr(&aevt,keyDirectObject,typeChar,
  85.             (*h)+selStart,selEnd-selStart)) goto Done;
  86.  
  87.         if (err = AEPutParamPtr(&aevt,keyUser,typeChar,&name[1],name[0]))
  88.             goto Done;
  89.  
  90.         err = AESend(&aevt,&reply,kAENoReply,kAENormalPriority,
  91.             kAEDefaultTimeout,NULL,NULL);
  92.         }
  93.  
  94. Done:
  95.     if (err != 0)
  96.         SysBeep(1);
  97.  
  98.     // it's safe to do this here, since disposing a NULL descriptor is harmless
  99.     AEDisposeDesc(&aevt);
  100.     AEDisposeDesc(&reply);
  101.     AEDisposeDesc(&addr);
  102.  
  103.     HSetState(h,hstate);
  104.     RestoreA4();
  105. }
  106.